package com.jingxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.MatchInfo;
import com.jingxing.pojo.TrainingSignIn;
import com.jingxing.service.MatchInfoService;
import com.jingxing.service.TrainingSignInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/trainingSignIn")
@Api(value = "API", tags = {"签到相关"})
@UserLoginToken
public class TrainingSignInController {
    @Autowired
    private TrainingSignInService trainingSignInService;

    @Autowired
    private MatchInfoService matchInfoService;


    @PostMapping("/signIn")
    @ApiOperation("签到")
    public ApiResult signIn(String playerId, Integer trainingLevel) {
        if (StringUtils.isBlank(playerId)) return ApiResult.parameterError("选手ID不能为空");
        if (trainingLevel == null) return ApiResult.parameterError("签到阶段不能为空");

        // 先判断签到人是否晋级到该阶段
        QueryWrapper<MatchInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("player_id", playerId);
        if (trainingLevel == 1) {
            queryWrapper.eq("competition_level", 8);
        }
        else {
            queryWrapper.eq("competition_level", 0);
        }
        MatchInfo one = matchInfoService.getOne(queryWrapper);
        if (one == null) return ApiResult.no(one, "选手未晋级培训阶段");

        // 判断一天内是否重复签到
        QueryWrapper<TrainingSignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("player_id", playerId);
        wrapper.eq("training_level", trainingLevel);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        wrapper.like("create_time", format);
        TrainingSignIn trainingSignIn = trainingSignInService.getOne(wrapper);
        if (trainingSignIn != null) return ApiResult.no(null, "已签到完毕，无需重复签到");

        // 签到
        TrainingSignIn sign = new TrainingSignIn();
        sign.setPlayerId(playerId);
        sign.setTrainingLevel(trainingLevel);
        boolean save = trainingSignInService.save(sign);
        return ApiResult.result(save);
    }

    @PostMapping("/getSignInByPlayerId")
    @ApiOperation("通过选手id查询对应签到记录")
    public ApiResult getSignInByPlayerId(String playerId) {
        if (StringUtils.isBlank(playerId)) return ApiResult.parameterError("选手ID不能为空");
        QueryWrapper<TrainingSignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("player_id", playerId);
        List<TrainingSignIn> list = trainingSignInService.list(wrapper);
        return ApiResult.ok(list);
    }


}
